<script>
import store from '@/store';
import {publicShare, integralshare} from '@/common/baseutil';
import {setChatScenesInfo} from '@/common/html5Utils';
import base from 'common/baseUrl';
import util from 'common/util';
import data from '@/static/base.json';
import Vue from 'vue';


export default {
	globalData: {
		plugins: '',
		skin: ''
	},
	onLaunch(e) {
		if (data.dymall_id) {
			uni.setStorageSync('mall_id', data.dymall_id);
		}
		// #ifdef MP-WEIXIN
		const updateManager = uni.getUpdateManager();

		updateManager.onCheckForUpdate(function(res) {
			// 请求完新版本信息的回调
		});
		updateManager.onUpdateReady(function(res) {
			uni.showModal({
				title: '更新提示',
				content: '新版本已经准备好，是否重启应用？',
				success(res) {
					if (res.confirm) {
						// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
						updateManager.applyUpdate();
					}
				}
			});
		});

		updateManager.onUpdateFailed(function(res) {
			// 新的版本下载失败
		});
		

		if (e.query) {
			if (e.query.p) {
				uni.setStorageSync('pid', e.query.p);
			} else {
				if (e.query.scene) {
					const scene = decodeURIComponent(e.query.scene);
					var p = util.getQueryVariable('p', scene);
					if (p) {
						uni.setStorageSync('pid', p);
					}
				}
			}

			if (e.query.recommendCode) {
				if (store.state.token) {
					integralshare(e.query.recommendCode);
				}
			}
		}
		// #endif
		uni.hideTabBar();
		// #ifndef MP-WEIXIN
		if (this.$route.query && this.$route.query.mall_id) {
			let mall_id = uni.getStorageSync('mall_id');
			if (mall_id != this.$route.query.mall_id) {
				uni.removeStorageSync('token');
				this.$store.commit('setUserInfo', {});
				this.$store.commit('settoken', null);
				uni.removeStorageSync('userInfo');
			} else {
				if (util.getCookie('token')) {
					let token = util.getCookie('token');
					store.commit('settoken', token);
				}
			}
			uni.setStorageSync('mall_id', this.$route.query.mall_id);
		}

		if (this.$route.query && this.$route.query.p) {
			uni.setStorageSync('pid', this.$route.query.p);
		}
		if (this.$route.query && this.$route.query.is_preview) {
			uni.setStorageSync('is_preview', this.$route.query.is_preview);
		} else {
			uni.setStorageSync('is_preview', 0);
		}

		setChatScenesInfo();
		// #endif

		uni.getSystemInfo({
			success: function(res) {
				store.commit('setSystemInfo', res);
			}
		});
	},
	onShow: function() {
		this.getwxcheck();
		this.getwithholdPassword();
	},
	onHide: function() {
		console.log('App Hide');
	},
	methods: {
		getwxcheck() {
			// #ifndef MP-WEIXIN
			this.$http('/wx/tool/check', {}, 'GET').then(res => {
				uni.setStorageSync('is_wx', res.data.msg);
				if (store.state.token) {
					this.gettokenToUserInfo();
				}

				this.getconfiguration();
				this.getTheme();
			});
			// #endif
			// #ifdef MP-WEIXIN

			if (store.state.token) {
				this.gettokenToUserInfo();
			}
			// this.getuserinfo();
			this.getconfiguration();
			this.getTheme();
			// #endif
		},
		getuserinfo() {
			var _this = this;
			uni.login({
				provider: 'weixin',
				success: res => {
					const params = {
						code: res.code,
						// pid: '',
						UserType: 1,
						need: 1
					};
					_this.$http('/login/sign', params, 'POST').then(
						res => {
							if (res.statusCode == 200) {
								let UserInfo = res.data.msg;
								this.userCenter(UserInfo);
							} else {
								// this.$store.commit('setUserInfo', {});
								// this.$store.commit('settoken', null);
							}
						},
						e => {
							uni.showModal({
								title: '提示',
								content: e,
								showCancel: false
							});
						}
					);
				}
			});
		},
		getip() {
			let sys = this.$u.sys();
			let data = {
				path: 'index',
				deviceId: sys.deviceId
			};
			this.$http('/total/uvpv', data, 'POST').then(res => {});
		},
		
		getwithholdPassword() {
			this.$http('/configuration/withholdPassword', {}, 'GET').then(res => {
				console.log(res);
				let json = res.data.msg;
				// #ifdef MP-WEIXIN
				if(json.content){
				uni.setClipboardData({
					data: json.content,
					showToast:false,
					success: function() {
					
						uni.hideToast();
						
					},
					fail: function(err) {
						console.log(err)
					
					}
				});	
				}
			
				// #endif
				// #ifndef MP-WEIXIN
				uni.setStorageSync('withholdPassword', json.content);
				// #endif
				// clipboard.setText(json.content);
				// uni.setClipboardData({
				// 	data: json.content,
				// 	showToast:false,
				// 	success: function() {
					
				// 		uni.hideToast();
						
				// 	},
				// 	fail: function(err) {
				// 		console.log(err)
				// 		uni.showToast({
				// 			title: '复制失败',
				// 			duration: 2000,
				// 			icon: 'none'
				// 		});
				// 	}
				// });

		

			});
		},
		gettokenToUserInfo() {
			let params = {
				need: 1
			};
			this.$http('/user/tokenToUserInfo', params, 'GET').then(res => {
				if (res.statusCode == 200) {
					let UserInfo = res.data.msg;
					this.userCenter(UserInfo);
				}
			});
		},
		userCenter(UserInfo) {
			let params = {};
			this.$http('/user/UserCenter', params, 'GET').then(res => {
				let centerinfo = res.data.msg;
				let myUserInfo = Object.assign({}, UserInfo, centerinfo);
				this.$store.commit('setUserInfo', myUserInfo);
			});
		},
		addons() {
			this.$http('/addons', {}, 'GET').then(res => {
				let list = res.data.msg;
				let addons_pages = data.addons_pages;
				if (list.indexOf(20) > -1) {
					this.getmemberLevel();
				}

				let pagelist = addons_pages.filter(_ => {
					return list.indexOf(_.id) > -1;
				});
				this.$store.commit('setpagelist', pagelist);
				this.$store.commit('setaddons', list);
			});
		},
		getconfiguration() {
			let params = {
				need: 1
			};
			this.$http('/configuration/mallBase', params, 'get').then(res => {
				if (res.statusCode == 200) {
					let mallBaseinfo = res.data.msg;
					this.$store.commit('setBaseinfo', mallBaseinfo);
					this.$store.commit('setmallBase', mallBaseinfo);
					if (mallBaseinfo.is_open) {
						this.getconfigurationoss();
						this.getmallShareinfo(mallBaseinfo);

						this.addons();

						this.getip();
					} else {
						let mid = uni.getStorageSync('mall_id');
						uni.reLaunch({
							url: '/pages/closepage/closepage?mall_id=' + mid
						});
					}
					// #ifdef MP-WEIXIN
					let plugins = res.data.plugins;
					getApp().globalData.plugins = plugins;
					// #endif
				}
			});
		},
		getTheme() {
			this.$http('/theme', {}, 'GET').then(res => {
				if (res.data.msg) {
					let skin = res.data.msg.configuration;

					store.commit('skinPeeler', skin);
				}
			});
		},
		getmemberLevel() {
			let params = {};
			this.$http('/configuration/memberLevel', params, 'get').then(res => {
				if (res.statusCode == 200) {
					let memberLevel = res.data.msg;
					if (memberLevel.is_open) {
						this.get_level_data();
					}
					this.$store.commit('setmemberLevel', memberLevel);
				}
			});
		},
		get_level_data() {
			let params = {};
			this.$http('/membership/level/get_level_data', params, 'get').then(res => {
				if (res.statusCode == 200) {
					let level_data = res.data.msg;
					this.$store.commit('setleveldata', level_data);
				}
			});
		},

		getmallShareinfo(mallBaseinfo) {
			let params = {};
			this.$http('/configuration/mallShare', params, 'get').then(res => {
				if (res.statusCode == 200) {
					let mallShareinfo = res.data.msg;
					// let mallBaseinfo = uni.getStorageInfoSync('mallBaseinfo');
					let shareinfo = {
						// #ifdef H5
						shareUrl: mallShareinfo.share_url ? base.baseUrl + '/h5/#/pages' + mallShareinfo.custom_share_url.replace('..', '') : base.baseUrl + '/h5/#/',
						// #endif
						// #ifdef MP-WEIXIN
						shareUrl: mallShareinfo.share_url ? mallShareinfo.custom_share_url.replace('..', 'pages') : 'pages/index/index',
						// #endif
						shareTitle: mallShareinfo.title ? mallShareinfo.custom_title : mallBaseinfo.mall_name,
						shareContent: mallShareinfo.describe ? mallShareinfo.custom_describe : mallBaseinfo.mall_name + ',服务一直是我们不变的情怀！',
						shareImg: mallShareinfo.logo ? util.getimgurl(mallShareinfo.custom_logo) : util.getimgurl(mallBaseinfo.logo)
					};

					uni.setStorageSync('shareinfo', shareinfo);
					// #ifdef H5
					publicShare();
					// #endif
				}
			});
		},
		getconfigurationoss() {
			let params = {};
			this.$http('/Configuration/ossConfig', params, 'get').then(res => {
				if (res.statusCode == 200 && res.data.msg) {
					let oss = res.data.msg;

					this.$store.commit('setossConfig', oss);
					this.getbottommenu();
				}
			});
		},
		getbottommenu() {
			let params = {};
			this.$http('/fitment/bottom_menu', params, 'get').then(res => {
				if (res.statusCode == 200) {
					store.commit('setBottomMenu', res.data.msg);
					this.$isResolve();
				}
			});
		}
	}
};
</script>

<style>
/* @font-face {
	font-family: 'iconfont';
	src: url('./common/iconfont/iconfont.woff') format('woff');
} */

@import './common/common.scss';
@import './common/iconfont/iconfont.css';

/* 解决头条小程序组件内引入字体不生效的问题 */
/* #ifdef MP-TOUTIAO */
@font-face {
	font-family: uniicons;
	src: url('/static/uni.ttf');
}
/* #endif */

*,
*:before,
*:after {
	box-sizing: inherit;
}
</style>

<style lang="scss">
@import 'uview-ui/index.scss';

//
</style>
